<template>
  <div>
    <el-steps :active="active" finish-status="success">
      <el-step title="账户注册"></el-step>
      <el-step title="信息填写"></el-step>
      <el-step title="账户激活"></el-step>
    </el-steps>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="邮箱 " prop="email">
        <el-input placeholder="请输入邮箱" v-model="ruleForm.email"  :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="checkNum">
        <el-input placeholder="请输入验证码" v-model="ruleForm.checkNum"></el-input>
        <el-button class="sendEmail" type="primary"  :disabled="isSend" @click="sendEmail()">发送邮件</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">完成</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "ActiveAccount.vue",
  data() {
    return {
      active: 2,
      isSend:false,
      ruleForm: {
        email: '',
        checkNum:'',
        userId:''
      },
      rules: {
        checkNum: [
          { required: true, message: '请输入六位验证码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          debugger;
          this.$axios.post("/email/check", this.ruleForm).then(res => {
            if (res.data.data === true) {
              this.$axios.post("/changeStatus/1/" + this.ruleForm.userId).then(res => {
                if (res.data.code === 200) {
                  this.$message({
                    showClose: true,
                    message: '激活成功',
                    type: 'success'
                  });
                  _this.$router.push("/success")
                } else {
                  this.$message.error(res.data.message);
                }
              })
            } else {
              this.$message.error(res.data.message);
              return false;
            }
          });
        }else{
              this.$message.error("验证码错误");
            }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    sendEmail(){
      //拿到邮箱和用户名作为唯一认证  去发送邮件
      const email= this.ruleForm.email
      const username=this.ruleForm.username
      //调用异步请求
      this.$axios.post("/email/sendEmail",this.ruleForm).then(res => {
        if(res.data.code===200){
          this.$message(res.data.message);
          this.isSend=true;
        }else{
          this.$message.error(res.data.message);
        }
      })
      }

    },

  created() {
    const userId = this.$route.params.userId
    const email = this.$route.params.email
    this.ruleForm.email=email
    this.ruleForm.userId=userId
  }

}
</script>
<style>
</style>